package com.demo.service;

import com.demo.mapper.ArticleMapper;
import com.demo.pojo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("ArticleService")
public class ArticleService implements ArticleMapper {
    @Resource
    private ArticleMapper am;
    @Resource
    private ArticleContentService acs;

    @Resource
    private ArticleKeywordService aks;

    @Override
    public long countByExample(ArticleExample example) {
        return am.countByExample(example);
    }

    @Override
    public int deleteByExample(ArticleExample example) {
        return am.deleteByExample(example);
    }

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return am.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(Article record) {
        return am.insert(record);
    }

    @Override
    public int insertSelective(Article record) {
        return am.insertSelective(record);
    }

    @Override
    public List<Article> selectByExample(ArticleExample example) {
        return am.selectByExample(example);
    }

    @Override
    public Article selectByPrimaryKey(Integer id) {
        return am.selectByPrimaryKey(id);
    }

    @Override
    public int updateByExampleSelective(Article record, ArticleExample example) {
        return am.updateByExampleSelective(record,example);
    }

    @Override
    public int updateByExample(Article record, ArticleExample example) {
        return am.updateByExample(record, example);
    }

    @Override
    public int updateByPrimaryKeySelective(Article record) {
        return updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(Article record) {
        return updateByPrimaryKey(record);
    }

    public List<Article> getArticlesWithFullInfo(){
        List<Article> articleList = am.selectByExample(new ArticleExample());
        makeFull(articleList);
        return articleList;
    }

    public List<Article> search(String content){
        ArticleExample example = new ArticleExample();
        ArticleExample.Criteria criteria = example.createCriteria();
        criteria.andTitleLike(content);
        List<Article> articleList = am.selectByExample(example);
        makeFull(articleList);
        return articleList;
    }

    private List<Article> makeFull(List<Article> list){
        for(Article article : list){
            article.setArticleContent(getContentByArticleId(article.getId()));
            article.setArticleKeyword(getArticleKeywords(article.getId()));
        }
        return list;
    }
    public Article getArticleWithFullInfo(Integer id){
        Article article = am.selectByPrimaryKey(id);
        article.setArticleContent(getContentByArticleId(id));
        article.setArticleKeyword(getArticleKeywords(id));
        return article;
    }

    public PageInfo<Article> selectWithPage(ArticleExample example, Params params){
        PageHelper.startPage(params.getPageNum(),params.getPageSize());
        List<Article> list = selectByExample(example);
        return PageInfo.of(list);
    }
    public ArticleContent getContentByArticleId(Integer articleId){
        ArticleContentExample ace =new ArticleContentExample();
        ArticleContentExample.Criteria criteria =ace.createCriteria();
        criteria.andArticleIdEqualTo(articleId);
        List<ArticleContent> articleContents = acs.selectByExampleWithBLOBs(ace);
        if(articleContents.isEmpty()){
            return null;
        }
        return articleContents.get(0);
    }

    public List<ArticleKeyword> getArticleKeywords(Integer articleId){
        ArticleKeywordExample ake =new ArticleKeywordExample();
        ArticleKeywordExample.Criteria criteria = ake.createCriteria();
        criteria.andArticleIdEqualTo(articleId);
        return aks.selectByExample(ake);
    }

}
